package com.bawei.module_launcher.user

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.telephony.TelephonyManager
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.module_launcher.FrameActivity
import com.bawei.module_launcher.R
import com.bawei.module_launcher.user.entity.RequestEntity
import com.bawei.module_launcher.user.model.PhoneService
import com.blankj.utilcode.util.SPUtils
import kotlinx.coroutines.launch
@Route(path = "/launcher/login")
class LoginActivity : AppCompatActivity() {
    private val service:PhoneService by lazy{PhoneService()}
    private var number:String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
    }
    //获得当前手机号 -》访问接口给该手机发送验证码
    fun sendCode(view: View) {
        //TODO 1:获得当前手机号
        val tm: TelephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
        ) {
            return
        }
        number = tm.line1Number//手机号码
        Toast.makeText(this,"本机号码 $number",Toast.LENGTH_SHORT).show()
       //TODO 2:访问接口给该手机发送验证码
        lifecycleScope.launch{
            val sendCode = service.sendCode(RequestEntity(number!!, ""))
            if(sendCode.statusCode == 200){
                Log.d("ytx", "发送验证码成功: ")
            }else{
                Log.d("ytx", "发送验证码失败: ${sendCode.msg}")
            }
        }
    }

    fun verfiryCode(view: View) {
        val code = findViewById<EditText>(R.id.et).text.toString()
        lifecycleScope.launch{
            val sendCode = service.verfiryCode(RequestEntity(number!!, code))
            if(sendCode.statusCode == 200){
                Log.d("ytx", "验证码校验成功 登陆成功: ${sendCode.msg}")
                val intent = Intent(this@LoginActivity, FrameActivity::class.java)
                startActivity(intent)
                //登陆成功后返回用户信息：SP存储是否为管理员
                if(sendCode.msg!!.contains("管理员")){
                    SPUtils.getInstance().put("admin",true)
                }else {
                    SPUtils.getInstance().put("admin",false)
                }
                //登陆过
                SPUtils.getInstance().put("login",true)

            }else{
                Log.d("ytx", "发送验证码失败: ${sendCode.msg}")
            }
        }

    }
    //手动输入手机号
    fun sendCode2(view: View) {
        number = findViewById<EditText>(R.id.number).text.toString()
        //TODO 2:访问接口给该手机发送验证码
        lifecycleScope.launch{
            val sendCode = service.sendCode(RequestEntity(number!!, ""))
            if(sendCode.statusCode == 200){
                Log.d("ytx", "发送验证码成功: ")

            }else{
                Log.d("ytx", "发送验证码失败: ${sendCode.msg}")

            }
        }
    }
}